package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.CatalogService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@MapperScan
@RequestMapping("/manage")
public class ManageController {
    @Autowired
    Account account;
    @Autowired
    AccountService accountService;
    @Autowired
    HttpSession session;
    @Autowired
    CatalogService catalogService;

    @GetMapping("/signOn")
    public String getSellerSignOn(Model model){
        model.addAttribute("account",account);
        return "manage/signOn";
    }
//    @PostMapping("/signOn")
//    public String SellerSignOnSubmit(@ModelAttribute Account account, HttpServletRequest request, Model model){
//        String password=request.getParameter("password");
//        String username = request.getParameter("username");
//        this.account = accountService.getAccount(account.getUsername(),password);
//        if(this.account!=null && this.account.getUsername() == "a" && this.account.getPassword() == "a")
//        {
//            session.setAttribute("account",this.account);
//            session.setAttribute("managename",this.account.getUsername());
//            session.setAttribute("managepassword",this.account.getPassword());
//            session.setAttribute("flag",1);
////            List<Category> categoryList = catalogService.getCategoryList();
////            model.addAttribute("categoryList",categoryList);
//            return "manage/managemain";
//        }
//        else {
//            //登录失败
//            model.addAttribute("msg", "用户名或密码错误");
//        }
//        return "manage/signOn";
//    }

    //登出页面
    @GetMapping("/signOff")
    public String signOff(){
        session.setAttribute("account",null);
//        session.setAttribute("cart",null);
        return "catalog/main";
    }

    @GetMapping("/viewCategory")
    public String viewCategory()
    {
        List<Category> categoryList = catalogService.getCategoryList();
        session.setAttribute("categoryList",categoryList);
        return "manage/catalog/manageCategory";
    }

    @GetMapping("/deleteCategory")
    @ResponseBody
    public List<Category> deleteCategory(String categoryId){
        catalogService.deleteCategoryById(categoryId);
        List<Category> categoryList = catalogService.getCategoryList();
        return categoryList;
    }
    @GetMapping("/viewEditCategory")
    public String viewEditCategory(String categoryId,Model model){
        Category category = catalogService.getCategory(categoryId);
        model.addAttribute("category",category);
        return "manage/catalog/categoryEdit";

    }
    @GetMapping("/editCategory")
    public List<Category> editCategory(String categoryId,String name,String description){
        Category category = catalogService.getCategory(categoryId);
        category.setName(name);
        category.setDescription(description);
        catalogService.editCategory(category);
        List<Category> categoryList = catalogService.getCategoryList();
        return categoryList;
    }
    @GetMapping("/viewProduct")
    public String viewProduct(String categoryId, Model model)
    {
        Category category = catalogService.getCategory(categoryId);
        List<Product> productList = catalogService.getProductListByCategory(categoryId);
        session.setAttribute("category",category);
        model.addAttribute("productList",productList);
        return "manage/catalog/manageProduct";
    }
    @GetMapping("/deleteProduct")
    @ResponseBody
    public List<Product> deleteProduct(String categoryId,String productId){ ;
        catalogService.deleteProduct(productId);
        List<Product> productList = catalogService.getProductListByCategory(categoryId);
        return productList;
    }
    @GetMapping("/vieweditProduct")
    public String viewEditProduct(String productId,Model model){
        Product product = catalogService.getProduct(productId);
        model.addAttribute("product",product);
        return "manage/catalog/productEdit";
    }
    @GetMapping("/viewItem")
    public String viewItem(String productId,Model model)
    {
        Product product = catalogService.getProduct(productId);
        List<Item> itemList = catalogService.getItemListByProduct(productId);
        session.setAttribute("product",product);
        model.addAttribute("itemList",itemList);
        return "manage/catalog/manageItem";
    }
    @GetMapping("/itemEdit")
    public String itemEditGet(String id,Model model)
    {
        Item item = catalogService.getItem(id);
        model.addAttribute("item",item);
        return "manage/catalog/itemEdit";
    }
    @PostMapping("/itemEdit")
    public String itemEditPost(Model model, HttpServletRequest request, Item item, HttpSession httpSession)
    {
        catalogService.updateItem(item);
        catalogService.updateInventory(item);
//            String productId = item.getProductId();
//        Product product = catalogService.getProduct(productId);
//        List<Item> itemList = catalogService.getItemListByProduct(productId);
//        model.addAttribute("product",product);
//        model.addAttribute("itemList",itemList);
        model.addAttribute("msg", "修改成功");
        return "manage/catalog/itemEdit";
    }

//    @GetMapping("/itemDelete")
//    public String itemDelete(String id)
//    {
//        Item item = catalogService.getItem(id);
//        ReItem reItem = new ReItem();
//        reItem.setItemId(item.getItemId());
//        reItem.setProductId(item.getProductId());
//        reItem.setListPrice(item.getListPrice());
//        reItem.setUnitCost(item.getUnitCost());
//        reItem.setSupplierId(item.getSupplierId());
//        reItem.setStatus(item.getStatus());
//        reItem.setAttribute1(item.getAttribute1());
//        reItem.setProduct(item.getProduct());
//        reItem.setQuantity(item.getQuantity());
//        reItem.setRecovered(new Date());
////        catalogService.insertReItem(reItem);
//        catalogService.deleteItem(item);
//        return "manage/catalog/ManageItem";
//    }

    @GetMapping("/itemAdd")
    public String itemAddGet(String productId,Model model)
    {
        System.out.println(productId);
        session.setAttribute("productId",productId);
        return "manage/catalog/itemAdd";
    }
    @PostMapping("/itemAdd")
    public String itemAddPost(Model model, HttpServletRequest request, Item item, HttpSession httpSession)
    {
        catalogService.addItem(item);
        model.addAttribute("msg","增加成功");
        return "manage/catalog/itemAdd";
    }


}
